package de.baumann.browser.present.duobao

import android.content.Context
import android.util.Log
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import de.baumann.browser.api.OdinObserver
import de.baumann.browser.api.net.vo.duobao.*
import de.baumann.browser.base.BasePresenter
import de.baumann.browser.iview.duobao.IAddressView
import de.baumann.browser.model.dzgx
import de.baumann.browser.model.getAddress
import de.baumann.browser.model.tjdz
import java.io.BufferedReader
import java.io.InputStreamReader
import java.lang.StringBuilder

class AddressPresenter : BasePresenter<IAddressView>() {

    fun getSHDZ() {
        getAddress().subscribe(object : OdinObserver<Address>(view) {
            override fun onSuccess(data: Address?) {
                if (data == null) {
                    return
                }

                view?.setName(data.name)
                view.setPhone(data.phone)
                view.setAddress(data.address)
                view.setAddInfo(data.addressInfo)
            }
        })
    }


    fun initAddressPicker(context: Context) {
        val provinceItems = mutableListOf<AddressInfoPO>()
        val cityItems = mutableListOf<MutableList<AddressInfoPO>>()
        val areaItems = mutableListOf<MutableList<MutableList<AddressInfoPO>>>()

        //Json2Bean
        val pcaCodeList = Gson().fromJson<MutableList<PCACodePO>>(getAssetsFileText(context, "area.json"), object : TypeToken<MutableList<PCACodePO>>() {}.type)
        //遍历省
        pcaCodeList.forEach { pcaCode ->
            //存放省内市区
            val cityList = mutableListOf<AddressInfoPO>()
            //存放省内所有辖区
            val areaList = mutableListOf<MutableList<AddressInfoPO>>()
            //遍历省内市区
            pcaCode.children.forEach { cCode ->
                //添加省内市区
                cityList.add(AddressInfoPO(cCode.code, cCode.name))
                //存放市内辖区
                val areas = mutableListOf<AddressInfoPO>()
                //添加市内辖区
                cCode.children.forEach { addressInfo ->
                    areas.add(addressInfo)
                }
                areaList.add(areas)
            }
            //添加省份
            provinceItems.add(AddressInfoPO(pcaCode.code, pcaCode.name))
            //添加市区
            cityItems.add(cityList)
            //添加辖区
            areaItems.add(areaList)
        }
        //显示选择器
        view?.showAddressPicker(provinceItems, cityItems, areaItems)
    }

    fun duobaotjdz() {
        tjdz(view!!.n, view.p, view.a, view.i)
                .subscribe(object : OdinObserver<Any>(view) {
                    override fun onSuccess(data: Any?) {
                        view?.dztjcg()
                    }
                })
    }


    fun duobaogxdz() {
        dzgx(view!!.n, view.p, view.a, view.i)
                .subscribe(object : OdinObserver<Any>(view) {
                    override fun onSuccess(data: Any?) {
                        view?.dztjcg()
                    }
                })
    }
}